﻿using System.Web.Mvc;
using System.Web.Security;
using TestBat.Frontend.Controllers.ViewData.Login;

namespace TestBat.Frontend.Controllers
{
    public class LoginController : Controller
    {
        public ActionResult Index(bool? loginFailed)
        {
            var vd = new IndexViewData();

            if (loginFailed.HasValue)
                vd.LoginFailed = loginFailed.Value;
            else
                vd.LoginFailed = false;

            return View(vd);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(string username, string password, string returnUrl)
        {
            bool valid = Membership.ValidateUser(username, password);

            if (valid)
            {
                FormsAuthentication.SetAuthCookie(username, false);

                if (string.IsNullOrEmpty(returnUrl))
                    return Redirect(FormsAuthentication.DefaultUrl);
                else
                    return Redirect(returnUrl);
            }
            else
                return RedirectToAction("Index", new { loginFailed = true });
        }

        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Index");
        }
    }
}
